<!DOCTYPE html>

<html>
	<head>
		<title>Materials</title>
		<script type="text/javascript" src="../libs/three.js"></script>
		<script type="text/javascript" src="../libs/jquery-1.9.0.js"></script>
		<script type="text/javascript" src="../libs/stats.js"></script>
		<script type="text/javascript" src="../libs/dat.gui.js"></script>
		<style >
			body{
				/* set margin to 0 and overflow to hidden, to use the complete page */

				margin: 0;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<!-- Div which will hold the Output -->
		<div id="WebGL-output">
		</div>

		<!-- Div which will show the stats -->
		<div id="Stats-output">
		</div>

		<!-- Javascript code that runs our Three.js examples -->
		<script type="text/javascript">
			//once everything is loaded, we run our Three.js stuff.
			$(function () { 
 				var stats = initStats();
 				
 				//here we'll put the Three.js stuff
 				var scene = new THREE.Scene();
 				
 				//add camera and renderer
 				var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
 				
 				var renderer = new THREE.WebGLRenderer();
 				renderer.setClearColor(0xEEEEEE, 1.0);
 				renderer.setSize(window.innerWidth, window.innerHeight);
 				renderer.shadowMapEnabled = true;

 				//add Light
 				var ambientLight = new THREE.AmbientLight(0x0c0c0c);
 				scene.add(ambientLight);

 				var spotLight = new THREE.SpotLight(0xffffff);
 				spotLight.castShadow = true;
 				spotLight.position.set(-40, 60, -10);
 				scene.add(spotLight);

 				//add plane
 				var planeGeo = new THREE.PlaneGeometry(60,40,1,1);
 				var planeMtr = new THREE.MeshLambertMaterial({color: 0xffffff});
 				var plane = new THREE.Mesh(planeGeo, planeMtr);
 				plane.rotation.x = -0.5 * Math.PI;
		        plane.position.x = 0;
		        plane.position.y = 0;
		        plane.position.z = 0;
		        plane.receiveShadow = true;
		        scene.add(plane);


		        camera.position.x = -30;
		        camera.position.y = 40;
		        camera.position.z = 30;
		        camera.near = 50;
		        camera.far = 60;
		        camera.lookAt(scene.position);
		        document.getElementById("WebGL-output").appendChild(renderer.domElement);


		        //define materials
		        var basicMtr = new THREE.MeshBasicMaterial({color: 0xffaacc});
		        var depthMtr = new THREE.MeshDepthMaterial({color: 0xffaacc});
		        var normalMtr = new THREE.MeshNormalMaterial();
		        var lambertMtr = new THREE.MeshLambertMaterial({color: 0xffaacc});
		        var phongMtr = new THREE.MeshPhongMaterial({color: 0xffaacc});
		        var lineBasicMtr = new THREE.LineBasicMaterial({color: 0xffaacc});
		        var lineDashedMtr = new THREE.LineDashedMaterial({color: 0xffaacc});

		        //add box
		        var boxGeo = new THREE.BoxGeometry(4,4,4);
		        var box = new THREE.Mesh(boxGeo, basicMtr);
		        box.castShadow = true;
		        scene.add(box);

		        var controls = new function() {
		        	this.rotation = true;
		        	this.material = "basicMtr";
		        	this.color = 0xffaacc;
		        	this.visible = true;
		        	this.opacity = 1;
		        	this.transparent = true;

		        }


		        var gui = new dat.GUI();
		        gui.add(controls, "rotation");
		        gui.add(controls, "material", ["basicMtr", "depthMtr", "normalMtr", "lambertMtr", "phongMtr"]).onChange(function (e){
		        	switch(e){
		        		case "basicMtr":
		        			box.material = basicMtr;
		        			break;
		        		case "depthMtr":
		        			box.material = depthMtr;
		        			break;
		        		case "normalMtr":
		        			box.material = normalMtr;
		        			break;
		        		case "lambertMtr":
		        			box.material = lambertMtr;
		        			break;
		        		case "phongMtr":
		        			box.material = phongMtr;
		        			break;
		        	}
		        });
		        gui.addColor(controls, "color").onChange(function (e){
		        	box.material.color = new THREE.Color(e);
		        });
		        gui.add(controls, "visible").onChange(function (e){
		        	box.material.visible = e;
		        });
		        gui.add(controls, "opacity", 0, 1).onChange(function (e){
		        	box.material.opacity = e;
		        });
		        gui.add(controls, "transparent").onChange(function (e){
		        	box.material.transparent = e;
		        });




 				//add animation
 				var step = 0.03
 				var sum = 0;
 				render();
 				function render(){
 					stats.update();

 					sum += step;
 					box.position.x = -10 * Math.sin(sum);
 					box.position.y = Math.abs(10 * Math.cos(sum));
 					box.position.z = 10 * Math.sin(sum);

 					if(controls.rotation){
 						box.rotation.x += step;
 						box.rotation.y += step;
 						box.rotation.z += step;
 					}

 					requestAnimationFrame(render);
 					renderer.render(scene, camera);
 				};

			});

			function initStats(){
				var stats = new Stats();
				stats.setMode(0);
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.lefe = '0px';
				stats.domElement.style.top = '0px';
				$("#Stats-output").append(stats.domElement);
				return stats;
			}

		</script>

	</body>
</html>